package com.elastic.util;

import com.elastic.model.Product;
import com.elastic.model._MappingKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import javax.sql.DataSource;

/**
 * @author: 李德才
 * @description:
 * @create: 2021-04-05 16:11
 **/
public class MyDataSource {

    private static final DruidPlugin dbPlugin;

    static {
        Prop prop = PropKit.use("AppConfig.properties").appendIfExists("AppConfig-pro.properties");
        dbPlugin = new DruidPlugin(prop.get("mysql.url"), prop.get("mysql.userName"), prop.get("mysql.passWord"), prop.get("mysql.driver"));
        dbPlugin.start();
        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(dbPlugin);
//        activeRecordPlugin.addMapping("product", "id", Product.class);
        _MappingKit.mapping(activeRecordPlugin);
        activeRecordPlugin.start();
    }

    public static DataSource getDataSource() {
        return dbPlugin.getDataSource();
    }

    public static DruidPlugin getDbPlugin() {
        return dbPlugin;
    }
}
